home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-18 | 6.9 KB | 322 lines | [TEXT/MMCC] |
- /*
- File: DocWindow.cp
-
- Contains: A simple document window
-
- Written by: Dave Falkenburg
-
- Copyright: © 1993-94 by Dave Falkenburg, all rights reserved.
-
- Change History (most recent first):
-
-
- To Do: Figure out if any of these methods should move to TWindow
-
- */
-
- #include "DocWindow.h"
- #include "AppLib.h"
- #include <LowMem.h> // for LMGetCurApName()
- #include <ToolUtils.h> // for NumToString()
- #include <Icons.h>
- #include <Threads.h>
-
- unsigned long TDocWindow::fgUntitledTagCount = 0;
-
- static void SpinArrowsThreadProc(TDocWindow* pWindowToSpin)
- {
- while (true)
- {
- pWindowToSpin->SpinHeaderArrows();
- YieldToAnyThread();
- }
- }
-
- static void MyDrawGrowIcon(WindowPtr pWindow)
- {
- Rect aGrowRect = pWindow->portRect;
- RgnHandle aSaveClipRgn = NewRgn();
-
- GetClip(aSaveClipRgn);
-
- aGrowRect.top += kHeaderHeight;
- ClipRect(&aGrowRect);
- DrawGrowIcon(pWindow);
- SetClip(aSaveClipRgn);
- DisposeRgn(aSaveClipRgn);
- }
-
- TDocWindow::TDocWindow()
- : fDocFile(nil),
- fTHPrint(nil),
- fDirty(false)
- {
- fCurrentSpinningArrowIconID = kFirstSpinningArrowIconID;
-
- TDocWindow::fgUntitledTagCount++; // Starts out as zero but we want “Untitled-1”
- this->CreateWindow();
-
- if (gHasThreadManager)
- {
- OSErr err = NewThread(kCooperativeThread, (ThreadEntryProcPtr) SpinArrowsThreadProc, this, 0, 0, nil, &fSpinnerThreadID);
- if (err != noErr)
- DebugStr((StringPtr) "\pNewThread failed");
- }
- }
-
- TDocWindow::~TDocWindow()
- {
- if (fDocFile)
- delete fDocFile;
-
- if (fTHPrint)
- DisposeHandle((Handle)fTHPrint);
-
- // NEWS OF THE WEIRD!
- // If you pass true for recyleThread param, the heap block for the stack isn’t
- // disposed— it gets placed in the default cooperative thread pool of “premade”
- // threads.
- //
- // To optimize memory usage, I might just want to go ahead and create
- // a thread pool at application startup so we can avoid moving memory around
- // so much.
-
- if (gHasThreadManager)
- {
- OSErr err = DisposeThread(fSpinnerThreadID,nil,false);
- if (err != noErr)
- DebugStr((StringPtr) "\pDisposeThread failed");
- }
- }
-
- Boolean TDocWindow::IsDirty(void)
- {
- return fDirty;
- }
-
- void TDocWindow::SetDirty(Boolean theDirtyFlag)
- {
- fDirty = theDirtyFlag;
- }
-
- WindowPtr TDocWindow::MakeNewWindow(WindowPtr pBehindWindow)
- {
- WindowPtr pWindow = GetNewColorOrBlackAndWhiteWindow(kDocWindowTemplateID, nil, pBehindWindow);
-
- if (pWindow)
- {
- Str63 aTitleString;
- // make the window exciting & different
- // Can’t call nudge because it assumes the window is visible!
- // Nudge(fgUntitledTagCount * kHeaderHeight,fgUntitledTagCount * kHeaderHeight);
-
- GetWTitle(pWindow,aTitleString);
- if (*aTitleString)
- {
- Str31 aNumberString;
-
- NumToString(fgUntitledTagCount, aNumberString);
- BlockMove(&aNumberString[1], &aTitleString[1+*aTitleString], *aNumberString);
- *aTitleString += *aNumberString;
- }
- SetWTitle(pWindow, aTitleString);
- }
- return pWindow;
- }
-
-
- void TDocWindow::Activate(Boolean /* theIsActivating */)
- {
- MyDrawGrowIcon(fWindow);
- }
-
- void TDocWindow::AdjustCursor(EventRecord * /* pEvent */)
- {
- }
-
- void TDocWindow::Draw(void)
- {
- EraseRect(&fWindow->portRect);
-
- MoveTo(0,kHeaderHeight-3);
- LineTo(fWindow->portRect.right,kHeaderHeight-3);
- MoveTo(0,kHeaderHeight-1);
- LineTo(fWindow->portRect.right,kHeaderHeight-1);
-
- MyDrawGrowIcon(fWindow);
- }
-
- void TDocWindow::Click(EventRecord * /* pEvent */)
- {
- this->Select();
- }
-
- void TDocWindow::SetupMenus(void)
- {
- TWindow::SetupMenus();
- if (IsDirty())
- EnableMenuItem(mFile, iSave, true);
- EnableMenuItem(mFile, iSaveAs, true);
- EnableMenuItem(mFile, iPageSetup, true);
- EnableMenuItem(mFile, iCustomPageSetup, true);
- EnableMenuItem(mFile, iPrint, true);
- EnableMenuItem(mFile, iPrintOneCopy, true);
- }
-
- void TDocWindow::AdjustForNewWindowSize(Rect* pOldSize, Rect* /* pNewSize*/)
- {
- Rect aScrollbarRect;
-
- // Invalidate the old vertical scroll bar
- aScrollbarRect = *pOldSize;
- aScrollbarRect.left = aScrollbarRect.right - kScrollbarWidth;
- InvalRect(&aScrollbarRect);
-
- // Invalidate the old horizontal scroll bar
- aScrollbarRect.left = pOldSize->left;
- aScrollbarRect.top = pOldSize->bottom - kScrollbarWidth;
- InvalRect(&aScrollbarRect);
-
- MyDrawGrowIcon(fWindow);
- }
-
- Boolean TDocWindow::SaveDocument(void)
- {
- return true;
- }
-
- Boolean TDocWindow::SaveDocumentAs(void)
- {
- return true;
- }
-
- void TDocWindow::HandleMenuCommand(short theMenuID, short theMenuItem)
- {
- Boolean aHandled = true;
- switch(theMenuID)
- {
- case mFile :
- switch(theMenuItem)
- {
- default : aHandled = false; break;
- }
- break;
-
- default : aHandled = false; break;
- }
- if ( ! aHandled)
- TWindow::HandleMenuCommand(theMenuID, theMenuItem);
- }
-
- void TDocWindow::PageSetupDlg(Boolean /*theCustomFlag*/)
- {
- if (fTHPrint == nil)
- fTHPrint = (THPrint)NewHandleClear(sizeof(TPrint));
- PrOpen();
- if ( ! PrValidate(fTHPrint))
- PrintDefault(fTHPrint);
- if (PrStlDialog(fTHPrint))
- ;
- PrClose();
- }
-
- void TDocWindow::PrintDlg(Boolean /*theDialogFlag*/)
- {
- if (fTHPrint == nil)
- fTHPrint = (THPrint)NewHandleClear(sizeof(TPrint));
- PrOpen();
- if ( ! PrValidate(fTHPrint))
- PrintDefault(fTHPrint);
- if (PrJobDialog(fTHPrint))
- SysBeep(1); // <= print now
- PrClose();
- }
-
- Boolean TDocWindow::Close(void)
- {
- if (this->IsDirty())
- {
- StandardCloseResult aResult;
- Str255 aDocTitle;
-
- GetWTitle(this->fWindow, aDocTitle);
- aResult = StandardCloseDocument(LMGetCurApName(), aDocTitle, false, false);
-
- if (aResult == kCancelSaveDocument)
- return false;
- if (aResult == kSaveDocument)
- if (SaveDocument() == false)
- return false;
- }
- return TWindow::Close();
- }
-
- void TDocWindow::SetDocFile(FSSpec* pFSSpec)
- {
- fDocFile = new TDocFile;
- if (fDocFile)
- {
- fDocFile->SetFSSpec(pFSSpec);
- fDocFile->SetForkAvail(true, true);
- if (pFSSpec)
- SetWTitle(fWindow, pFSSpec->name);
- }
- }
-
- OSErr TDocWindow::DragEnterWindow(DragReference theDrag)
- {
- return this->DragInWindow(theDrag);
- }
-
- OSErr TDocWindow::DragInWindow(DragReference theDrag)
- {
- RgnHandle aHiliteRgn = NewRgn();
- Point aMouseLoc;
- Point aPinnedMouseLoc;
-
- SetRectRgn( aHiliteRgn,
- fWindow->portRect.left,
- fWindow->portRect.top + kHeaderHeight,
- fWindow->portRect.right-kScrollbarWidth,
- fWindow->portRect.bottom-kScrollbarWidth);
-
- (void) GetDragMouse(theDrag,&aMouseLoc,&aPinnedMouseLoc);
- GlobalToLocal(&aMouseLoc);
-
- if (PtInRgn(aMouseLoc,aHiliteRgn))
- ShowDragHilite(theDrag,aHiliteRgn,true);
- else
- HideDragHilite(theDrag);
-
- DisposeRgn(aHiliteRgn);
- return noErr;
- }
-
- OSErr TDocWindow::DragLeaveWindow(DragReference theDrag)
- {
- HideDragHilite(theDrag);
- return noErr;
- }
-
- OSErr TDocWindow::HandleDrop(DragReference /* theDrag */)
- {
- return noErr;
- }
-
- void TDocWindow::SpinHeaderArrows()
- {
- CSavePort aSavePort(fWindow);
- Rect aSpinRect;
-
- fCurrentSpinningArrowIconID++;
-
- if (fCurrentSpinningArrowIconID > kLastSpinningArrowIconID)
- fCurrentSpinningArrowIconID = kFirstSpinningArrowIconID;
-
- aSpinRect.top = fWindow->portRect.top;
- aSpinRect.left = fWindow->portRect.left+4;
- aSpinRect.bottom = aSpinRect.top + 16;
- aSpinRect.right = aSpinRect.left + 16;
- (void) PlotIconID(&aSpinRect,atNone,ttNone,fCurrentSpinningArrowIconID);
- }
-